package xuhaijia.lanou3g.autohome.find;
//                .-~~~~~~~~~-._       _.-~~~~~~~~~-.
//            __.'              ~.   .~              `.__
//          .'//                  \./                   \\`.
//        .'//            为什么坚持  想一想当初             \\`.
//      .'// .-~"""""""~~~~-._     |     _,-~~~~"""""""~-.  \\`.
//    .'//.-"                 `-.  |  .-'                 "-. \\`.
//  .'//______.============-..   \ | /   ..-============._______\\`.
//.'//____________________________\|/_____________________________\\`.
// 
//                             

import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import xuhaijia.lanou3g.autohome.R;
import xuhaijia.lanou3g.autohome.activity.RadioActivity;
import xuhaijia.lanou3g.autohome.activity.WebViewActivity;
import xuhaijia.lanou3g.autohome.base.BaseFragment;
import xuhaijia.lanou3g.autohome.base.NetTool;
import xuhaijia.lanou3g.autohome.base.URLValues;
import xuhaijia.lanou3g.autohome.custom.HorizontialListView;
import xuhaijia.lanou3g.autohome.custom.LimitedBuyTextView;
import xuhaijia.lanou3g.autohome.find.groupbuy.GroupBuyActivity;
import xuhaijia.lanou3g.autohome.findcar.UsedCarFragment;
import xuhaijia.lanou3g.autohome.turnpage.TurnPageCenter;
import xuhaijia.lanou3g.autohome.turnpage.TurnPageCenterAdapter;

/**
 * Created by dllo on 16/8/15.
 */
public class FindFragment extends BaseFragment implements View.OnClickListener {

    private TurnPageCenter turnPageCenter;
    private FrameLayout frameLayout;
    private RecyclerView functionRv;
    private FunctionAdapter functionAdapter;
    private ImageView activityFirstIv;
    private ImageView activitySecondIv;
    private ImageView activityThirdIv;
    private ImageView twoActivitiesFirstIv;
    private ImageView twoActivitiesSecondIv;
    private RecyclerView guessLikeRv;
    private GuessLikeAdapter guessLikeAdapter;
    private RecyclerView recForMeRv;
    private RecForMeAdapter recForMeAdapter;
    private GoodsRecAdapter goodsRecAdapter;
    private RecyclerView goodsRecRv;
    private LinearLayout limitedBuyLl;
    private RelativeLayout noLimitedBuy;
    private FunctionBean functionBean;
    private LimitedBuyAdapter limitedBuyAdapter;
    private HorizontialListView horLv;
    Intent pagerIntent;
    private LinearLayout moreActivity;

    public void setPagerIntent(Intent pagerIntent) {
        this.pagerIntent = pagerIntent;
        pagerIntent.setClass(getContext(), WebViewActivity.class);
        startActivity(pagerIntent);
    }

    public FunctionBean getFunctionBean() {
        return functionBean;
    }

    public void setFunctionBean(FunctionBean functionBean) {
        this.functionBean = functionBean;
    }

    @Override
    protected int setLayout() {
        return R.layout.fragment_find;
    }

    @Override
    protected void initView() {
        frameLayout = bindView(R.id.find_framelayout);
        functionRv = bindView(R.id.find_function_rv);
        guessLikeRv = bindView(R.id.find_guesslike_rv);
        goodsRecRv = bindView(R.id.find_goodsrec_rv);
        recForMeRv = bindView(R.id.find_recforme_rv);
        turnPageCenter = new TurnPageCenter(mContext);
        horLv = bindView(R.id.fragment_fin_horizontiallv);
        activityFirstIv = bindView(R.id.find_activity_first);
        activitySecondIv = bindView(R.id.find_activity_second);
        activityThirdIv = bindView(R.id.find_activity_third);
        twoActivitiesFirstIv = bindView(R.id.find_two_activities_first);
        twoActivitiesSecondIv = bindView(R.id.find_two_activities_second);
        limitedBuyLl = bindView(R.id.limited_buy_timer);
        noLimitedBuy = bindView(R.id.fragment_find_nolimitedbuy);
        activityFirstIv.setOnClickListener(this);
        activitySecondIv.setOnClickListener(this);
        activityThirdIv.setOnClickListener(this);
        twoActivitiesFirstIv.setOnClickListener(this);
        twoActivitiesSecondIv.setOnClickListener(this);
        moreActivity = bindView(R.id.find_more_activity);
        moreActivity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), MoreActActivity.class);
                startActivity(intent);
            }
        });

    }

    @Override
    protected void initData() {
        functionAdapter = new FunctionAdapter(mContext);
        GridLayoutManager manager = new GridLayoutManager(mContext, 5);
        functionRv.setLayoutManager(manager);
        mNetTool.getNetData(URLValues.FUNCTION_FIND_URL, FunctionBean.class, new NetTool.NetListener<FunctionBean>() {
            @Override
            public void onSuccess(FunctionBean functionBean) {
                functionAdapter.setFunctionBean(functionBean);
                functionRv.setAdapter(functionAdapter);
                mNetTool.getImage(functionBean.getResult().getImageads().getImageadsinfo().get(0).getImgurl(), activityFirstIv);
                mNetTool.getImage(functionBean.getResult().getImageads().getImageadsinfo().get(1).getImgurl(), activitySecondIv);
                mNetTool.getImage(functionBean.getResult().getImageads().getImageadsinfo().get(2).getImgurl(), activityThirdIv);
                try {
                    mNetTool.getImage(functionBean.getResult().getActivitylist().get(0).getImgurl(), twoActivitiesFirstIv);
                    mNetTool.getImage(functionBean.getResult().getActivitylist().get(1).getImgurl(), twoActivitiesSecondIv);
                } catch (Exception e) {

                }
                setFunctionBean(functionBean);
                functionAdapter.setFunctionClickListener(new FunctionAdapter.FunctionClickListener() {
                    @Override
                    public void onItemClick(int position) {
                        Intent intent = new Intent();
                        switch (position) {
                            case 0:
                                intent.setClass(getContext(), RadioActivity.class);
                                break;
                            case 1:
                                intent.setClass(getContext(), WebViewActivity.class);
                                intent.putExtra("url", URLValues.CAR_STORE_URL);
                                break;
                            case 2:
                                intent.setClass(getContext(), WebViewActivity.class);
                                intent.putExtra("url", URLValues.MUCH_BUY_URL);
                                break;
                            case 3:
                                intent.setClass(getContext(), GroupBuyActivity.class);
                                break;
                            case 4:
                                intent.setClass(getContext(), UsedCarFragment.class);
                                break;
                            case 5:
                                intent.setClass(getContext(), WebViewActivity.class);
                                intent.putExtra("url", URLValues.ADD_ROW_URL);
                                break;
                            case 7:
                                intent.setClass(getContext(), WebViewActivity.class);
                                intent.putExtra("url", URLValues.PROVIDER_HOME_URL);
                                break;
                            case 8:
                                intent.setClass(getContext(), WebViewActivity.class);
                                intent.putExtra("url", URLValues.LOW_INSURANCE_URL);
                                break;

                        }

                        startActivity(intent);
                    }
                });
            }

            @Override
            public void onError(String errorMsg) {

            }
        });
        limitedBuyAdapter = new LimitedBuyAdapter(mContext);
        mNetTool.getNetData(URLValues.LIMITEDBUY_FIND_URL, LimitedBuyBean.class, new NetTool.NetListener<LimitedBuyBean>() {
            private LimitedBuyTextView limited;

            @Override
            public void onSuccess(final LimitedBuyBean limitedBuyBean) {
                if (limitedBuyBean.getResult().getLimitedbuy().getLimitedbuyinfo().size() == 0) {
                    noLimitedBuy.setVisibility(View.INVISIBLE);
                    noLimitedBuy.setLayoutParams(new LinearLayout.LayoutParams(0, 0));
                } else {
                    noLimitedBuy.setVisibility(View.VISIBLE);
                    limited = new LimitedBuyTextView(mContext, limitedBuyBean.getResult().getLimitedbuy().getEndtime());
                    limitedBuyLl.addView(limited.initTime());
                    limitedBuyAdapter.setLimitedBuyBean(limitedBuyBean);
                    horLv.setAdapter(limitedBuyAdapter);
                    horLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                            Intent intent = new Intent(getContext(), WebViewActivity.class);
                            intent.putExtra("url", limitedBuyBean.getResult().getLimitedbuy().getLimitedbuyinfo().get(position).getUrl());
                            startActivity(intent);
                        }
                    });
                }
            }

            @Override
            public void onError(String errorMsg) {

            }
        });
        GridLayoutManager guessLikeManager = new GridLayoutManager(mContext, 2);
        guessLikeRv.setLayoutManager(guessLikeManager);
        GridLayoutManager recForMeManager = new GridLayoutManager(mContext, 2);
        recForMeRv.setLayoutManager(recForMeManager);
        LinearLayoutManager goodsRecManager = new LinearLayoutManager(mContext);
        goodsRecRv.setLayoutManager(goodsRecManager);
        goodsRecAdapter = new GoodsRecAdapter(mContext);
        guessLikeAdapter = new GuessLikeAdapter(mContext);
        recForMeAdapter = new RecForMeAdapter(mContext);
        mNetTool.getNetData(URLValues.GUESS_FIND_URL, GuessLikeBean.class, new NetTool.NetListener<GuessLikeBean>() {
            @Override
            public void onSuccess(GuessLikeBean guessLikeBean) {
                guessLikeAdapter.setGuessLikeBean(guessLikeBean);
                recForMeAdapter.setGuessLikeBean(guessLikeBean);
                goodsRecAdapter.setGuessLikeBean(guessLikeBean);
                guessLikeRv.setAdapter(guessLikeAdapter);
                recForMeRv.setAdapter(recForMeAdapter);
                goodsRecRv.setAdapter(goodsRecAdapter);
            }

            @Override
            public void onError(String errorMsg) {

            }
        });
    }

    @Override
    public void load() {

    }

    @Override
    public void refresh() {

    }

    @Override
    public void onResume() {
        super.onResume();
        mNetTool.getNetData(URLValues.CAROUSE_FIND_URL, TurnBean.class, new NetTool.NetListener<TurnBean>() {
            @Override
            public void onSuccess(TurnBean turnBean) {
                turnPageCenter.setTurnBean(turnBean);
                turnPageCenter.setFindFragment(FindFragment.this);
                frameLayout.addView(turnPageCenter.getView());
            }

            @Override
            public void onError(String errorMsg) {

            }
        });
    }

    @Override
    public void onStop() {
        super.onStop();
        turnPageCenter.stop();
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        intent.setClass(getContext(), WebViewActivity.class);
        switch (v.getId()) {
            case R.id.find_activity_first:
                intent.putExtra("url", getFunctionBean().getResult().getImageads().getImageadsinfo().get(0).getUrl());
                break;
            case R.id.find_activity_second:
                intent.putExtra("url", getFunctionBean().getResult().getImageads().getImageadsinfo().get(1).getUrl());
                break;
            case R.id.find_activity_third:
                intent.putExtra("url", getFunctionBean().getResult().getImageads().getImageadsinfo().get(2).getUrl());
                break;
            case R.id.find_two_activities_first:
                intent.putExtra("url", getFunctionBean().getResult().getActivitylist().get(0).getUrl());
                break;
            case R.id.find_two_activities_second:
                intent.putExtra("url", getFunctionBean().getResult().getActivitylist().get(1).getUrl());
                break;
        }
        startActivity(intent);
    }
}
